/**
 * 建议尽量全字段或者区域多字段进行匹配, 避免误触
 */
// Import("ocr.js")
// Import("utils.js")
// Import("ggUtils.js")


var APP_NAME_TTSSJSB = '头条搜索极速版'
var PACK_NAME_TTSSJSB = 'com.ss.android.article.lite'
var MAIN_PAGE_FLAG_TTSSJSB = '首页'
var ENTER_CENTER_PAGE_FLAG_TTSSJSB = '任务'
var CENTER_PAGE_FLAG_TTSSJSB = '现金收益'

function skip_主界面的弹窗_TTSSJSB() {
    //添加
    // var detects = opencv.findImagesEx('图色195094.cv');
    // if (detects != null) {
    //     agentOrHidClick(detects[0])
    // }

    //x

    var detects = opencv.findImagesEx('图色413193.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }
    //x2
    var detects = opencv.findImagesEx('图色397542.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }

    // //x2
    // var detects = opencv.findImagesEx('图色415843.cv');
    // if (detects != null) {
    //     agentOrHidClick(detects[0])
    // }


    agentOrHidClick(ocrText('三天内不再', '天内不', '坚持退出', '以后再说', '暂不开启', '不开启', '取消'))
    agentOrHidClick(ocrTextMoreInRegionComplete(0, 0.5, 1, 1, '添加'))

}

function get惊喜奖励() {

    var flagjxgg = ocrText('恭喜获得', '惊喜奖励', '喜奖励', '恭喜获')
    // if (flagjxgg) {
    //     getMaxNumber
    // }
    if (flagjxgg) {
        var minnum = getMinNumByNearBy(flagjxgg)
        if (minnum < 10) {
            setExcuteFunc('start_收集金币_TTSSJSB')
            return
        }
        var moreFlag = ocrText('看视频再', '看广告再', '看视频领', '看广告领')
        if (getDectNumber(moreFlag) > 1500) {
            watchGG_多次看广告(moreFlag)
        }
    }
}

function skip_任务中心的弹窗_TTSSJSB() {
    get惊喜奖励()

    //找图返回目标数组
    var detectsclose2 = opencv.findImagesEx('图色962765.cv');
    if (detectsclose2 != null) {
        agentOrHidClick(detectsclose2[0])
    }
    //x
    var detectsclose = opencv.findImagesEx('图色617190.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }

    var detects右上角中心 = opencv.findImagesEx('图色649101.cv');
    if (detects右上角中心 != null) {
        agentOrHidClick(detects右上角中心[0])
    }

    //找图返回目标数组
    var detects银行卡提箱 = opencv.findImagesEx('图色591248.cv');
    if (detects银行卡提箱 != null) {
        agentOrHidClick(detects银行卡提箱[0])

        //找图返回目标数组
        var detects确定不要 = opencv.findImagesEx('图色144563.cv');
        if (detects确定不要 != null) {
            agentOrHidClick(detects确定不要[0])
        }
    }


    //找图返回目标数组
    var detectscloseall = opencv.findImagesEx('图色387120.cv');
    if (detectscloseall != null) {
        agentOrHidClick(detectscloseall[0])
    }

    agentOrHidClick(ocrText('三天内不再', '天内不', '残忍', '取消'))
    agentOrHidClick(ocrText('添加'))
}

function skip_逛街弹窗() {
    if (ocrText('逛街最多')) {
        var detects1金币 = opencv.findImagesEx('图色173861.cv');
        if (detects1金币 != null) {
            setExcuteFunc('start_收集金币_TTSSJSB')
        }

        var detectsclose = opencv.findImagesEx('图色617190.cv');
        if (detectsclose != null) {
            agentOrHidClick(detectsclose[0])
        }
    }
}

function back2center_TTSSJSB() {
    // ocrTextTrue('看文章或视频')
    if (!backUntilFindView(() => ocrText(CENTER_PAGE_FLAG_TTSSJSB, '新人福利', '日常任务', '或视频', '展开更多任务', '更多任务'))) {
        // enter_任务中心_TTSSJSB(true)
    }
}

// 启动任务
function start_app_TTSSJSB() {
    // app.closeApp(PACK_NAME_TTSSJSB)
    // sleep.millisecond(200)
    // app.openApp(PACK_NAME_TTSSJSB)
    printinfoOld('start_app_TTSSJSB')
    if (hasExcuteFunc('start_app_TTSSJSB')) {
        return false
    }

    if (!startApp(PACK_NAME_TTSSJSB)) {
        return false
    }

    // 跳过首页广告
    expectView(() => {
        return ocrTextMoreInRegion(0, 0.88, 1, 1, MAIN_PAGE_FLAG_TTSSJSB, '头条', '我的')
    }, (times) => {
        skip_主界面的弹窗_TTSSJSB()

        if (times > 4) {
            agentOrHidback()
            sleep.millisecond(500)
            // todo
        }
        sleep.millisecond(500)
    }, (findView) => {
        // agentOrHidClick(findView)
    }, () => {
        if (ocrText('个人信息保护', '处理个人信息')) {
            setExcuteFunc('start_app_TTSSJSB')
        }
    }, 4)

    if (hasExcuteFunc('start_app_TTSSJSB')) {
        return false
    }

    return true
}

// 进入任务中心
function enter_任务中心_TTSSJSB(iskip) {
    printinfoOld('enter_任务中心_TTSSJSB')

    if (iskip == undefined) iskip = false

    // 进入任务中心
    expectView(() => {
        return ocrTextMoreInRegion(0, 0.88, 1, 1, ENTER_CENTER_PAGE_FLAG_TTSSJSB)
    }, (times) => {
        skip_主界面的弹窗_TTSSJSB()

        if (times > 3) {
            agentOrHidback()
        }

        sleep.millisecond(500)
        SystemPrint.warn('未找到主界面期望标志位')
    }, (findView, stepTimes) => {
        if (stepTimes == 1) {
            skip_主界面的弹窗_TTSSJSB()
        }
        agentOrHidClick(findView)
    }, () => {
        // 失败后的方案 或者其它方案进入
        clickOffectY(5 / 12, 1, 60)
    }, 3)

    if (!ocrWaitForMoreTextMillisecond(CENTER_PAGE_FLAG_TTSSJSB, '新人福利', '日常任务', ',明日提现', '开宝箱得')) {
        clickOffectY(5 / 12, 1, 60)
    }
}


function open_签到_TTSSJSB() {
    printinfoOld('open_签到_TTSSJSB-------------------------------')
    if (hasExcuteFunc('open_签到_TTSSJSB')) {
        printinfoOld('open_签到_TTSSJSB 已经完成')
        return
    }

    // if (getExcuteFuncTimes('open_签到_TTSSJSB') >= 2) {
    //     return
    // }

    if (getExcuteFuncTimes('open_签到_TTSSJSB') > 1) {
        skip_任务中心的弹窗_TTSSJSB()
    }

    agentOrHidClick(ocrWaitForMoreTextMillisecond('直接领取', 6), 2, () => {
        setExcuteFunc('open_签到_TTSSJSB')
    })

    addExcuteFuncTimes('open_签到_TTSSJSB')
    // 后续的签到
    printinfoOld('后续的签到')
    // moveUp2FindWid((findView) => {
    //     agentOrHidClick(findView, 2, () => {
    //         agentOrHidClick(ocrText('直接领取'))

    //         agentOrHidClick(ocrTextInRegion(0, 0.5, 1, 0.75, '明日再来'), 1, () => {
    //             setExcuteFunc('open_签到_TTSSJSB')
    //             return
    //         })


    //         //找图返回目标数组
    //         var detects = opencv.findImagesEx('图色503251.cv');
    //         if (detects != null) {
    //             // printinfoOld(detects);
    //             agentOrHidClick(detects[0])
    //             setExcuteFunc('open_签到_TTSSJSB')
    //             return
    //         }

    //         if (!hasExcuteFunc('弹窗签到_TTSSJSB')) {
    //             agentOrHidClick(ocrText('立即签到'), 1, () => {
    //                 setExcuteFunc('弹窗签到_TTSSJSB')
    //             })
    //         }

    //         watchGG_多次看广告(ocrTextMoreInRegion(0, 0.5, 1, 0.95, '看视频最高', '看广告最高', '看广告领', '看视频领', '看视最高再领'))
    //         setExcuteFunc('open_签到_TTSSJSB')

    //     })

    // }, '每日签到', '签到')
    watchGG_多次看广告(ocrTextMoreInRegion(0, 0.5, 1, 0.95, '看视频最高', '看广告最高', '看广告领', '看视频领', '看视最高再领', '看视频再', '看广告再'))
    skip_任务中心的弹窗_TTSSJSB()

    if (ocrText('打开签到提醒')) {
        skip_任务中心的弹窗_TTSSJSB()
    }

    //x
    var detects = opencv.findImagesEx('图色765180.cv');
    if (detects != null) {
        agentOrHidClick(detects[0])
    }
}

function open_开宝箱_TTSSJSB() {
    printinfoOld('open_开宝箱_TTSSJSB-------------------------------')
    if (hasExcuteFunc('start_收集金币_TTSSJSB')) {
        return
    }

    skip_任务中心的弹窗_TTSSJSB()

    // agentOrHidClick(ocrText('开宝箱得金币'))
    agentOrHidClick(ocrText('开宝箱得金币'), 1, () => {

        var flag = ocrWaitForMoreTextMillisecondInRegion(0, 0.45, 1, 0.9, 6, '看视频再', '看广告再', '看视频领', '看广告领', '看视频最高', '看广告最高')
        if (flag == null && !ocrText('现金收益', '现金', '展开更多', '更多任务', '周周', '开宝箱')) {
            if (isInGGView()) {
                watchGG_多次看广告([], true)

                skip_逛街弹窗()
                return
            }
        }
        if (flag) {
            //找图返回目标数组
            var detects1金币 = opencv.findImagesEx('图色173861.cv');
            if (detects1金币 != null) {
                setExcuteFunc('start_收集金币_TTSSJSB')
                return
            }
            if (getDectNumber(flag) < 600) {
                printinfoOld('金币数量偏低了!!!')
                
                sleep.second(5)
                
                if (getExcuteFuncTimes('open_开宝箱_TTSSJSB') >= 1) {
                    setExcuteFuncInTime一段时间内不再执行('start_收集金币_TTSSJSB', 2 * 60 * 60)
                }
                addExcuteFuncTimes('open_开宝箱_TTSSJSB')

            }
            watchGG_多次看广告(flag)
        } else {
            agentOrHidClick(ocrText('看小视频再', '小视频再'), 2, () => {
                ocrWaitForMoreTextMillisecond('确认过眼神', '你想看的')

                expectView(() => ocrText('点击领取'), () => {
                    sleep.second(getRandomNum(5, 10))
                    agentSwipBig()
                }, (findView) => {
                    agentOrHidClick(findView)

                })
                back2center_TTSSJSB()
            })
        }


    })

    skip_逛街弹窗()
}

function watch_看广告_TTSSJSB() {
    printinfoOld('watch_看广告_TTSSJSB-------------------------------')

    if (hasExcuteFunc('start_收集金币_TTSSJSB')) {
        return
    }

    if (hasExcuteFunc('watch_看广告_TTSSJSB')) {
        return
    }
    if (getExcuteFuncTimes('watch_看广告_TTSSJSB') > 2) {
        return
    }

    if (!isEnableSwitch('jrttssb_kgg')) {
        return
    }
    skip_任务中心的弹窗_TTSSJSB()

    move2Top(7, 300, '现金收益', '赚赚卡', '活动收益')
    // 只执行一次
    moveUp2FindWidTxtMore((findView) => {
        watchGG_多次看广告(findView)
        setExcuteFunc('watch_看广告_TTSSJSB')
    }, '看广告赚金币', '看视频賺', '看视频', () => {
        return ocrText('有疑问', '活动规则', '继续上滑', '券后价', '直播中', '活动价')
    })

    addExcuteFuncTimes('watch_看广告_TTSSJSB')

    skip_逛街弹窗()
}

function go_走路赚钱_TTSSJSB() {
    printinfoOld('go_走路赚钱_TTSSJSB-------------------------------')
    if (hasExcuteFunc('start_收集金币_TTSSJSB')) {
        return
    }

    if (hasExcuteFunc('go_走路赚钱_TTSSJSB')) {
        return
    }
    if (hasExcuteFuncInTime是否在一段时间内执行过('go_走路赚钱_TTSSJSB')) {
        return
    }

    skip_任务中心的弹窗_TTSSJSB()
    setExcuteFuncInTime一段时间内不再执行('go_走路赚钱_TTSSJSB', 1 * 60 * 60)
    move2Top(7, 300, '现金收益', '赚赚卡', '活动收益')

    //找图返回目标数组
    var detects走路 = opencv.findImagesEx('图色111194.cv');
    if (detects走路 != null) {
        setExcuteFuncInTime一段时间内不再执行('go_走路赚钱_TTSSJSB', 4 * 60 * 60)
        agentOrHidClick(detects走路[0])

        if (!hasForverExcuteFunc('go_走路赚钱_TTSSJSB')) {
            agentOrHidClick(ocrText('开启'))

            agentOrHidClick(ocrWaitForMoreTextMillisecond('我知道了'), 2, () => {
                agentOrHidClickAbsoultX(ocrText('禁止', '取消', '拒绝'), screen.percentToWidth(0.75))
            })
        }

        get惊喜奖励()

        agentOrHidClick(ocrWaitForText('领取'), 2, () => {

            if (!hasForverExcuteFunc('go_走路赚钱_TTSSJSB')) {
                setForverExcuteFunc('go_走路赚钱_TTSSJSB')
            }

            watchGG_多次看广告(ocrText('看视频', '看广告'))
            setExcuteFunc('go_走路赚钱_TTSSJSB')
        })
    } else {

        moveUp2FindWidTxtMore((findView) => {
            setExcuteFuncInTime一段时间内不再执行('go_走路赚钱_TTSSJSB', 4 * 60 * 60)
            agentOrHidClick(findView)
            if (!hasForverExcuteFunc('go_走路赚钱_TTSSJSB')) {
                agentOrHidClick(ocrText('开启'))

                agentOrHidClick(ocrWaitForMoreTextMillisecond('我知道了'), 2, () => {
                    agentOrHidClickAbsoultX(ocrText('禁止', '取消'), screen.percentToWidth(0.75))
                })
            }
            get惊喜奖励()
            agentOrHidClick(ocrWaitForText('领取'), 2, () => {
                if (!hasForverExcuteFunc('go_走路赚钱_TTSSJSB')) {
                    setForverExcuteFunc('go_走路赚钱_TTSSJSB')
                }
                watchGG_多次看广告(ocrText('看视频', '看广告'))
                setExcuteFunc('go_走路赚钱_TTSSJSB')
            })
        }, '走路赚钱', '走路赚', () => {
            return ocrText('有疑问', '活动规则', '继续上滑', '券后价', '直播中', '活动价')
        })
    }

    // 只执行一次

    skip_逛街弹窗()

    back2center_TTSSJSB()
}

function go_吃饭补贴_TTSSJSB() {
    printinfoOld('go_吃饭补贴_TTSSJSB-------------------------------')
    if (hasExcuteFunc('start_收集金币_TTSSJSB')) {
        return
    }

    if (getEatingTime获取吃饭时间Flag()) {
        if (hasExcuteFunc('go_吃饭补贴_TTSSJSB' + getEatingTime获取吃饭时间Flag())) {
            return
        }

        skip_任务中心的弹窗_TTSSJSB()

        move2Top(7, 300, '现金收益', '赚赚卡', '活动收益')

        //找图返回目标数组
        var detects吃饭 = opencv.findImagesEx('图色816367.cv');
        if (detects吃饭 != null) {
            agentOrHidClick(detects吃饭[0])
            get惊喜奖励()
            // ocrText('直接领取', true)
            agentOrHidClick(ocrWaitForText('直接领取'), 2, () => {
                watchGG_多次看广告(ocrText('看视频', '看广告'))
                setExcuteFunc('go_吃饭补贴_TTSSJSB' + getEatingTime获取吃饭时间Flag())
            })
            if (!ocrText('再过')) {
                agentOrHidClick(ocrText('领取'), 2, () => {
                    watchGG_多次看广告(ocrText('看视频', '看广告'))
                    setExcuteFunc('go_吃饭补贴_TTSSJSB' + getEatingTime获取吃饭时间Flag())
                })
            }
        } else {
            moveUp2FindWidTxtMore((findView) => {
                agentOrHidClick(findView)
                get惊喜奖励()
                agentOrHidClick(ocrWaitForText('直接领取'), 2, () => {
                    watchGG_多次看广告(ocrText('看视频', '看广告'))
                    setExcuteFunc('go_吃饭补贴_TTSSJSB' + getEatingTime获取吃饭时间Flag())
                })
                if (!ocrText('再过')) {
                    agentOrHidClick(ocrText('领取'), 2, () => {
                        watchGG_多次看广告(ocrText('看视频', '看广告'))
                        setExcuteFunc('go_吃饭补贴_TTSSJSB' + getEatingTime获取吃饭时间Flag())
                    })
                }
            }, '吃饭补贴', '吃饭补', () => {
                return ocrText('有疑问', '活动规则', '继续上滑', '券后价', '直播中', '活动价')
            })
        }

        // 只执行一次

        skip_逛街弹窗()

        back2center_TTSSJSB()
    }
}

// 进行浏览任务
function start_浏览任务_TTSSJSB() {
    if (!start_app_TTSSJSB()) {
        return
    }

    skip_主界面的弹窗_TTSSJSB()

    //拍摄
    var detects = detects = opencv.findMultiColorEx('图色660381.cv');
    if (detects != null) {
        agentOrHidClickAbsoultX(detects[0], screen.percentToWidth(0.5))
    } else {
        //找色返回目标数组
        opencv.findImagesEx('图色810826.cv');
        if (detects != null) {
            agentOrHidClickAbsoultX(detects[0], screen.percentToWidth(0.5))
        }
    }



    // detects = opencv.findImagesEx('图色810826.cv');
    // if (detects != null) {
    //     agentOrHidClickAbsoultX(detects[0], screen.percentToWidth(0.5))
    // }

    skip_主界面的弹窗_TTSSJSB()

    if (ocrText('重试')) {
        agentOrHidback()
        sleep.second(1)
    }

    // 
    enter60分钟视频(getExcuteFuncTimes('start_浏览任务_TTSSJSB') + 1)
    skip_主界面的弹窗_TTSSJSB()


    agentOrHidClickOffectY(ocrWaitForMoreTextMillisecond('羊毛君分享师', '羊毛君', 10), 300)
    // 判断是否播放
    var detectsbf = opencv.findImagesEx('图色741102.cv');
    if (detectsbf == null) {
        agentOrHidClick(ocrText('次播放'))
    }

    addExcuteFuncTimes('start_浏览任务_TTSSJSB')
    // input.sendString('test')
}

// 看小说任务
// start_看小说任务_TTSSJSB()
function start_看小说任务_TTSSJSB() {
    printinfoOld('start_看小说任务_TTSSJSB')
    if (!isEnableSwitch('ttssjsb_kxs')) {
        return
    }
    if (hasExcuteFunc('start_看小说任务_TTSSJSB') || getExcuteFuncTimes('start_看小说任务_TTSSJSB') >= 2 || getExcuteFuncTimes('start_看小说任务_TTSSJSB_entertimes') > 4) {
        return
    }

    if (!start_app_TTSSJSB()) {
        return
    }

    skip_主界面的弹窗_TTSSJSB()
    enter_任务中心_TTSSJSB()


    sleep.second(2)
    skip_任务中心的弹窗_TTSSJSB()
    sleep.millisecond(200)

    expectView(() => {
        return ocrText('睡前看小说', '看小说赚金币') // ocrText('听相声', '睡前听') //
    }, () => {
        agentOrHidClick(ocrText('展开更多', '更多任务'))


        agentSwipSmallDown()
        sleep.second(1)
    }, (findView) => {
        agentOrHidClick(findView, 3)
    })


    var detectsclose = opencv.findImagesEx('图色335869.cv');
    if (detectsclose != null) {
        agentOrHidClick(detectsclose[0])
    }

    agentOrHidClick(ocrText('取消', '签到'))


    var flag = ocrWaitForMoreTextMillisecond('看小说赚钱', '小说赚钱', 10)
    if (flag == null) {
        //找色返回目标数组
        var detectsfalg = opencv.findMultiColorEx('图色714562.cv');
        if (detectsfalg != null) {
            flag = detectsfalg[0]
        }
    }
    if (flag) {
        agentOrHidClick(ocrText('万热度'), 2, () => {
            startThredTask(() => {
                ocrWaitForMoreTextMillisecond('加到桌面', '正版图书', '番茄小说', 10)
            }, (steptimes) => {
                clickOffectX(1, 0.5, 1, 0)
                if (steptimes % 2 == 0 || steptimes % 3 == 0) {
                    sleep.second(getRandomNum(5, 10))
                }
            }, 63 * 60)

            //找图返回目标数组
            var detects金币 = opencv.findImagesEx('图色536089.cv');
            if (detects金币 != null) {
                agentOrHidClick(detects金币[0])

                agentOrHidClick(ocrWaitForMoreTextMillisecond('去领取', 10))
            }

            addExcuteFuncTimes('start_看小说任务_TTSSJSB')

            // 红果刷金或者不开宝箱或者上限30
            if (isEnableSwitch('hongguoyanghao') || !isEnableSwitch('hg_openbx') || isEnableSwitch('sx_hg3')) {

            } else {
                if (getExcuteFuncTimes('start_看小说任务_TTSSJSB') >= 1) {
                    setExcuteFunc('start_看小说任务_TTSSJSB')
                }
            }
        })
    }

    addExcuteFuncTimes('start_看小说任务_TTSSJSB_entertimes')
    stopAllApp()
}

// start_收集金币_TTSSJSB()
function start_收集金币_TTSSJSB() {
    printinfoOld('start_收集金币_TTSSJSB')
    if (isEnableSwitch('ttssjsb_pbsjjb')) {
        return
    }
    if (hasExcuteFunc('start_收集金币_TTSSJSB')) {
        return
    }

    if (!isAllow允许登录(PACK_NAME_TTSSJSB)) {
        if (getExcuteFuncTimes('start_收集金币_TTSSJSB') == 0) {
            
            printwarn('头条搜索极速版启动失败或被屏蔽')
            sleep.second(10)
            
            addExcuteFuncTimes('start_收集金币_TTSSJSB')
        }
        return
    }
    currentAppPackName = PACK_NAME_TTSSJSB
    // closeApp(PACK_NAME_TTSSJSB)

    if (!start_app_TTSSJSB()) {
        return
    }

    addExcuteFuncTimes('start_收集金币_TTSSJSB')

    enter_任务中心_TTSSJSB()

    showEnterMainpageInfo展示进入任务弹窗信息()


    startThredTaskMaxTime超时结束(() => {
        open_签到_TTSSJSB()

        // open_开宝箱_TTSSJSB()
        open_开宝箱_TTSSJSB()

        watch_看广告_TTSSJSB()

        go_走路赚钱_TTSSJSB()

        go_吃饭补贴_TTSSJSB()


    }, 8 * 60)


    if (getExcuteFuncTimes('start_收集金币_TTSSJSB') >= 2) {
        move2Top(1)
        skip_任务中心的弹窗_TTSSJSB()
        agentSwipSmallDown()
        sleep.second(2)
        skip_任务中心的弹窗_TTSSJSB()
        var getNumbertr = getDectNumber(ocrTextInRegion(0, 0, 1, 0.25, '金币:'))
        if (getNumbertr < 2000 && getNumbertr > 0) {
            setExcuteFunc('start_收集金币_TTSSJSB')
        } else {
            if (getNumbertr > 2000) {
                addExcuteFuncTimes('start_收集金币_TTSSJSB_异常', 0)
            }
        }
    }

    stopAllApp()

}